﻿using System;
using SaleManagement.Web.App_Code;
using SaleManagement.Domains;
using SaleManagement.Services;
using DevExpress.Web.ASPxGridView;
using System.Web.UI.HtmlControls;

namespace SaleManagement.Web.Pages
{
  public partial class CustomerList : BasePage
  {
    #region Properties
    private string m_innerError = string.Empty;
    #endregion

    #region Events

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsCallback && !IsPostBack)
      {
        LoadPage();
        LoadCustomerList();
      }
    }

    protected void gvCustomer_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
      string[] para = e.Parameters.Split('|');
      if (para[0] == "Save")
      {
        m_innerError = "Save|" + SaveCustomer();
      }
      else if (para[0] == "Delete")
      {
        int id = 0;
        int.TryParse(para[1], out id);
        m_innerError = DeleteCustomer(id);
      }
      LoadCustomerList();
    }

    protected void gvCustomer_CustomJSProperties(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewClientJSPropertiesEventArgs e)
    {
      e.Properties["cpInnerError"] = m_innerError;
    }

    protected void gvCustomer_PageIndexChanged(object sender, EventArgs e)
    {
      LoadCustomerList();
    }

    protected void gvCustomer_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e)
    {
      if (e.DataColumn.Name == "Command")
      {
        ASPxGridView gvObject = sender as ASPxGridView;
        HtmlImage img = gvObject.FindRowCellTemplateControl(e.VisibleIndex, e.DataColumn, "imgEdit") as HtmlImage;
        if (img != null)
        {
          img.Attributes.Add("onclick", "onEdit(" + e.VisibleIndex + ")");
        }
      }
    }

    #endregion

    #region Methods

    private void LoadPage()
    {
      // txtPhone.Attributes.Add("onkeydown", "return onNumberEnter(this,event);");
    }

    private void LoadCustomerList()
    {
      string customerCode = null;
      string customerName = null;
      string phone = null;
      if (!string.IsNullOrEmpty(txtSearchCode.Text.Trim()))
      {
        customerCode = txtSearchCode.Text.Trim();
      }

      if (!string.IsNullOrEmpty(txtSearchName.Text.Trim()))
      {
        customerName = txtSearchName.Text.Trim();
      }

      if (!string.IsNullOrEmpty(txtSearchPhone.Text.Trim()))
      {
        phone = txtSearchPhone.Text.Trim();
      }

      TList<Customer> ds = new CustomerService().Search(customerCode, customerName, phone);
      gvCustomer.DataSource = ds;
      gvCustomer.DataBind();

    }

    private string SaveCustomer()
    {
      try
      {
        int id = 0;
        int.TryParse(hfCustomer.Value, out id);
        CustomerService service = new CustomerService();
        SaleManagement.Domains.Customer customer = null;
        if (id > 0)
        {
          customer = service.GetById(id);
          if (customer == null)
            return "Không tìm thấy khách hàng tương ứng";
        }
        else
        {
          customer = new SaleManagement.Domains.Customer();
        }
        customer.Name = Utilities.SafeString(txtName.Text);
        customer.Email = Utilities.SafeString(txtEmail.Text);
        customer.Address = Utilities.SafeString(txtAddress.Text);
        customer.Phone = Utilities.SafeString(txtPhone.Text);
        customer.Fax = Utilities.SafeString(txtFax.Text); ;
        service.Save(customer);
      }
      catch (Exception ex)
      {
        WriteLog("CustomerList.aspx::Save Customer", ex);
        return "Có lỗi trong quá trình lưu dữ liệu";
      }
      return string.Empty;
    }

    private string DeleteCustomer(int id)
    {
      try
      {
        new CustomerService().Delete(id);
      }
      catch (Exception ex)
      {
        WriteLog("CustomerList.aspx::Delet Customer", ex);
        return "Có lỗi trong quá trình lưu dữ liệu";
      }
      return string.Empty;
    }
    #endregion
  }
}
